//
//  NewsComment.swift
//  newsApp
//
//  Created by Tirel on 2020/5/12.
//  Copyright © 2020 Doo Psyist. All rights reserved.
//

import Foundation
import SwiftUI
import UIKit
struct NewsComment: View {
    @State var comment: String = ""
    @Environment(\.presentationMode) var presentation
    var newsListItem: NewsListItem

    var body: some View {
       
        VStack {
            HStack {
                Text("Comment")
                    .font(.system(size: 50,design: .rounded))
                    .foregroundColor(Color.orange)
                .bold()
                .shadow(radius: 1)
            }
            TextView(
                text: $comment

            )

                .frame(width: 350, height: 400)
                .cornerRadius(20)
                .onTapGesture {
                    let keyWindow = UIApplication.shared.connectedScenes
                        .filter({ $0.activationState == .foregroundActive })
                        .map({ $0 as? UIWindowScene })
                        .compactMap({ $0 })
                        .first?.windows
                        .filter({ $0.isKeyWindow }).first
                    keyWindow!.endEditing(true)

            }
            HStack {
                Button(action: {
                    print(self.newsListItem.id)
                    if !self.comment.isEmpty {
                        commentByid(self.newsListItem.id, self.comment)
                        self.presentation.wrappedValue.dismiss()
                    }
                    print(findNewsbyId(self.newsListItem.id))
                    
                }) {
                    Text("Submit")
                        .bold()
                        .font(.system(size: 30))
                        .frame(width: 200, height: 100)
                        .shadow(radius: 1)
                }
                Button(action: {
                    self.presentation.wrappedValue.dismiss()
                }


                ) {
                    Text("Cancel")
                        .bold()
                        .font(.system(size: 30))
                        .frame(width: 200, height: 100)
                        .foregroundColor(.orange)
                        .shadow(radius: 1)
                }
            }

        }
        
           



    }
}
func initPage() -> some View {
    NewsList()
}

fileprivate struct TextView: UIViewRepresentable {
    @Binding var text: String

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    func makeUIView(context: Context) -> UITextView {

        let myTextView = UITextView()
        myTextView.delegate = context.coordinator

        myTextView.font = UIFont(name: "HelveticaNeue", size: 15)
        myTextView.isScrollEnabled = true
        myTextView.isEditable = true
        myTextView.isUserInteractionEnabled = true
        myTextView.backgroundColor = UIColor(white: 0.0, alpha: 0.05)

        return myTextView
    }

    func updateUIView(_ uiView: UITextView, context: Context) {
        uiView.text = text
    }

    class Coordinator: NSObject, UITextViewDelegate {

        var parent: TextView

        init(_ uiTextView: TextView) {
            self.parent = uiTextView
        }

        func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
            return true
        }

        func textViewDidChange(_ textView: UITextView) {
            print("text now: \(String(describing: textView.text!))")
            if textView.markedTextRange == nil { self.parent.text = textView.text }
        }
    }
}


struct NewsComment_Previews: PreviewProvider {
    static var previews: some View {
        NewsComment(newsListItem: newsList[0])
    }
}
